'use strict';

class Detail {
    constructor(clusters, detail, serviceSrv, alarmSrv, ctgNotification, $state) {
        'ngInject';
        this._notification = ctgNotification;
        this.serviceSrv = serviceSrv;
        this.alarmSrv = alarmSrv;
        this._state = $state;
        this.selectList = [];
        this.nonSelectObError = true;
        this.ruleList = [];
        this.optsList = [{type: 'lt', name: '<'}, {type: 'gt', name: '>'}, {type: 'lte', name: '<='}, {
            type: 'gte',
            name: '>='
        }, {type: 'rt', name: '范围'}, {type: 'rte', name: '范围等于'}];
        this.clusters = clusters;
        this.clusterInst = clusters && clusters[0];
        this.data = {};
        this.loading = false;

        if (detail) {
            this.edit = true;

            this.getRCList(detail.service);
            this.getDPList(detail.service);

            this.data = {
                alertName: detail.alertName,
                phone: detail.phone,
                email: detail.email
            };
            this.ruleList = detail.ruleItem;
            this.showRc = detail.service && detail.service.length;
        } else {
            this.edit = false;

            this.getRCList();
            this.getDPList();
        }
        this.getAlertTypes();
    }

    getAlertTypes() {
        this.ruleLoading = true;
        this.alarmSrv.getAlertTypes({clusterCode: this.clusterInst.clusterCode}).then(data=> {
            this.typeList = data;
            if (!this.edit) {
                this.ruleList = [];
                this.ruleList.push(this.ruleTemplet());
            }
        }).catch(()=> {
            this.typeList = [];
            this._notification.error('获取告警类型列表数据有误.');
        }).finally(()=> {
            this.ruleLoading = false;
        });
    }

    ruleTemplet() {
        return {
            opts: this.optsList[0].type,
            alertItemId: this.typeList[0].id
        }
    }

    getRCList(ser) {
        this.rcList = null;
        this.serviceSrv.getRCList({
            perPage: 999,
            page: 1,
            code: this.clusterInst.clusterCode
        }).then(data=> {

            this.rcList = data.replicationControllers;

            if (this.edit) {
                angular.forEach(ser, (ele)=> {
                    angular.forEach(this.rcList, (rc)=> {
                        if (rc.name == ele.serviceName) {
                            rc.checked = true;
                            this.selectList.push(rc);
                        }
                    })
                });
            }
        }).catch(data=> {
            this.rcList = [];
            this._notification.error('获取RC服务列表数据有误.');
        });
    }

    getDPList(ser) {
        this.dpList = null;
        this.serviceSrv.getDPList({
            perPage: 999,
            page: 1,
            code: this.clusterInst.clusterCode
        }).then(data=> {
            this.dpList = data.deployments;

            if (this.edit) {
                angular.forEach(ser, (ele)=> {
                    angular.forEach(this.dpList, (dp)=> {
                        if (dp.objectMeta.name == ele.serviceName) {
                            dp.checked = true;
                            this.selectList.push(dp);
                        }
                    })
                });
            }
        }).catch(data=> {
            this.dpList = [];
            this._notification.error('获取DP服务列表数据有误.');
        });
    }

    cancel() {
        this._modalInstance.dismiss('cancel');
    }

    selectObItem(serItem) {
        if (serItem.checked) {
            this.selectList.splice(this.selectList.indexOf(serItem), 1);
            serItem.checked = false;
            if (!this.selectList.length) this.nonSelectObError = true;
        } else {
            serItem.checked = true;
            this.selectList.push(serItem);
            if (this.nonSelectObError) this.nonSelectObError = false;
        }
    }

    unObSel(index, elem) {
        if (this.rcList.indexOf(elem) > -1 || this.dpList.indexOf(elem) > -1) {
            elem.checked = false;
        }

        this.selectList.splice(index, 1);
        if (!this.selectList.length) {
            this.nonSelectObError = true;
            this.selListExpend = false;
        }
    }

    expendSelList() {
        this.selListExpend = !this.selListExpend;
    }

    save() {
        this.selectFormat = [];
        this.loading = true;
        angular.forEach(this.selectList, (ele, index)=> {
            this.selectFormat.push({
                serviceName: ele.objectMeta ? ele.objectMeta.name : ele.name,
                serviceNamespace: ele.objectMeta ? ele.objectMeta.namespace : ele.namespace
            });
        });
        var typeItem;
        angular.forEach(this.ruleList, ele => {
            typeItem = _.findWhere(this.typeList, {id: ele.alertItemId});
            ele.type = typeItem.type;
            ele.typeDescription = typeItem.description;
            ele.unit = typeItem.unit;
        });

        this.filter = {
            clusterCode: this.clusterInst.clusterCode,
            alertRuleDTO: {
                clusterID: this.clusterInst.clusterCode,
                alertName: this.data.alertName,
                phone: this.data.phone,
                email: this.data.email,
                id: this.edit ? this._state.params.id : '',
                ruleItem: this.ruleList,
                service: this.showRc ? this.selectFormat : []
            }
        };

        if (this.edit) {
            this.alarmSrv.editAlertRule(this.filter).then(data=> {
                this._state.go('alarm-k8s-pre');
            }).catch(()=> {
                this._notification.error('修改告警规则有误.');
            }).finally(()=> {
                this.loading = false;
            });
        } else {
            this.alarmSrv.addAlertRule(this.filter).then(data=> {
                this._state.go('alarm-k8s-pre');
            }).catch(()=> {
                this._notification.error('新增告警规则有误.');
            }).finally(()=> {
                this.loading = false;
            });
        }
    }


    addRule() {
        this.ruleList.push(this.ruleTemplet())
    }

    delRule($index) {
        if (this.ruleList.length > 1) {
            this.ruleList.splice($index, 1);
        }
    }
}


export default Detail